﻿using System;
using System.Data;
using ServerApi.Controllers;
using TuyennvLibrary.Utl;
using ServerApi.Models.Menu;

namespace ServerApi.administrator
{
    public partial class RoleManager : System.Web.UI.Page
    {
        public string title = "Thêm mới loại quyền";
        public int stt = 1;
        public string message = "";
        RoleModel roleModel = new RoleModel();
        TypeRoleModel typeModel = new TypeRoleModel();
        protected void Page_Load(object sender, EventArgs e)
        {
            AdminController.checkLogin(Session, Request, Response);
            AdminController.checkPermission(Page, this.GetType(), Session, Request, Response);
            if (!IsPostBack)
            {
                loadTypeRole();
                if (!string.IsNullOrEmpty(Request["id"]))
                {
                    int id = int.Parse(Request["id"]);
                    loadInfo(id);
                    title = "Update quyền";
                }
                else
                {
                    title = "Thêm mới quyền";
                }
                loadData();
            }
        }
        private void loadTypeRole()
        {
            DataTable dt = typeModel.GetListAll();
            ddlTypeRole.DataSource = dt;
            ddlTypeRole.DataTextField = "Name";
            ddlTypeRole.DataValueField = "id";
            ddlTypeRole.DataBind();

        }
        private void loadInfo(int id)
        {
            Role obj = roleModel.GetByID(id);
            loadInfo(obj);
        }
        private void loadInfo(Role obj)
        {
            txtName.Text = obj.Name;
            txtPage.Text = obj.PageName.Replace("/administrator/", "");
            ddlTypeRole.Text = obj.TypeRoleId.ToString();
            chkStatus.Checked = (obj.Status == 1) ? true : false;
        }
        private void loadData()
        {
            DataTable dt = roleModel.GetListAll();
            GrvList.DataSource = dt;
            GrvList.DataBind();
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                Role obj = new Role();
                obj.Name = txtName.Text;
                obj.PageName = txtPage.Text;
                if (!obj.PageName.Contains("/administrator/"))
                {
                    obj.PageName = "/administrator/" + obj.PageName;
                }
                obj.TypeRoleId = int.Parse(ddlTypeRole.Text);
                obj.Status = (chkStatus.Checked) ? 1 : 0;
                if (!string.IsNullOrEmpty(Request["id"]))
                {
                    obj.Id = int.Parse(Request["id"]);
                    roleModel.Update(obj);
                    message = Utilities.getSuccessMessage("Update quyền thành công");
                }
                else
                {
                    roleModel.Insert(obj);
                    message = Utilities.getSuccessMessage("Thêm mới quyền thành công");
                }
                loadData();
            }
            catch (Exception ex)
            {
                message = Utilities.getErrorMessage("Lỗi: " + ex.ToString());
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            loadData();
        }

        protected void GrvList_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
        {
            int id = int.Parse(GrvList.DataKeys[e.RowIndex].Value.ToString());

            if (roleModel.Delete(id))
            {
                loadData();
                Utilities.Alert(Page, this.GetType(), "Đã xóa quyền thành công");
            }
        }

    }
}